package com.gln.bloger.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.gln.bloger.dao.CategoryDao;
import com.gln.bloger.exception.BlogerException;
import com.gln.bloger.pojos.Category;
import com.gln.bloger.pojos.Website;
/**
 * 
 * @author renxj
 *
 */
public class CategoryDaoImpl extends BaseDaoImpl<Category> 
                                           implements CategoryDao{

	@SuppressWarnings("unchecked")
	public List<Category> getAllCategories(Website website)
			throws BlogerException {
		DetachedCriteria criteria = DetachedCriteria.forClass(Category.class);
		criteria.add(Restrictions.eq("website", website));
		criteria.addOrder(Order.desc("createTime"));
		List<Category> categories = getHibernateTemplate().findByCriteria(criteria);
		return categories;
	}

	@SuppressWarnings("unchecked")
	public List<Category> getCategories(Website website) throws BlogerException {
		DetachedCriteria criteria = DetachedCriteria.forClass(Category.class);
		criteria.add(Restrictions.eq("website", website));
		criteria.add(Restrictions.eq("secret", Boolean.FALSE));
		criteria.addOrder(Order.desc("createTime"));
		List<Category> categories = getHibernateTemplate().findByCriteria(criteria);
		return categories;
	}

	@SuppressWarnings("unchecked")
	public Category getByName(Website website, String cateName)
			throws BlogerException {
		DetachedCriteria criteria = DetachedCriteria.forClass(Category.class);
		criteria.add(Restrictions.eq("website", website));
		criteria.add(Restrictions.eq("name", cateName));
		List<Category> categories = getHibernateTemplate().findByCriteria(criteria);
		if(categories.size() > 0){
		    return categories.get(0);
		}else{
			return null;
		}
	}
}
